package com.groupbuying.currency.producer.service.impl;

import com.groupbuying.currency.producer.connector.PayInfoFeign;
import com.groupbuying.currency.producer.constants.CurrencyCustomerLogEnumConstants;
import com.groupbuying.currency.producer.constants.CurrencyEnumConstants;
import com.groupbuying.currency.producer.context.FilterContextHandler;
import com.groupbuying.currency.producer.dao.CurrencyCustomerLogDao;
import com.groupbuying.currency.producer.domain.CurrencyCustomerLogDO;
import com.groupbuying.currency.producer.dto.CurrencyCustomerLogDTO;
import com.groupbuying.currency.producer.service.CurrencyCustomerLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CurrencyCustomerLogServiceImpl implements CurrencyCustomerLogService {
    private static final Logger logger = LoggerFactory.getLogger(CurrencyCustomerLogServiceImpl.class);

    @Autowired
    private CurrencyCustomerLogDao currencyCustomerLogDao;
    @Autowired
    private PayInfoFeign payInfoFeign;

    @Override
    public CurrencyCustomerLogDO get(String logId) {
        return currencyCustomerLogDao.get(logId);
    }

    @Override
    public List<CurrencyCustomerLogDO> list(Map<String, Object> map) {
        return currencyCustomerLogDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return currencyCustomerLogDao.count(map);
    }

    @Override
    public int save(CurrencyCustomerLogDO currencyCustomerLog) {
        return currencyCustomerLogDao.save(currencyCustomerLog);
    }

    @Override
    public int update(CurrencyCustomerLogDO currencyCustomerLog) {
        return currencyCustomerLogDao.update(currencyCustomerLog);
    }

    @Override
    public int remove(String logId) {
        return currencyCustomerLogDao.remove(logId);
    }

    @Override
    public int batchRemove(String[] logIds) {
        return currencyCustomerLogDao.batchRemove(logIds);
    }

    @Override
    public List<CurrencyCustomerLogDTO> details(Map<String, Object> map) {
        logger.error("CurrencyCustomerLogServiceImpl.details()-----获取到的前台传递的参数信息：" + map.toString());
        try {
            Map conMap = new HashMap();
            conMap.put("customerId", map.get("consumerId"));
            List<CurrencyCustomerLogDO> customerLogDOList = currencyCustomerLogDao.list(conMap);
            logger.error("CurrencyCustomerLogServiceImpl.details()-----customerLogDOList：" + customerLogDOList.toString() + "==========" + customerLogDOList.size());
            List<CurrencyCustomerLogDTO> currencyCustomerLogList = new ArrayList<CurrencyCustomerLogDTO>();
            for (CurrencyCustomerLogDO currencyCustomerLogDO : customerLogDOList) {
                CurrencyCustomerLogDTO currencyCustomerLogDTO = new CurrencyCustomerLogDTO();
                /*判断支付类型*/
                String payId = currencyCustomerLogDO.getPayId();
                currencyCustomerLogDTO.setCreateTime(currencyCustomerLogDO.getCreateTime());
                /*判断积分类型为注册送积分*/
                if (payId.equals(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_REGISTER)) {
                    currencyCustomerLogDTO.setActivityType(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_REGISTERNAME);
                    currencyCustomerLogDTO.setStoreName(CurrencyCustomerLogEnumConstants.ENUM_STORENAME);
                    currencyCustomerLogDTO.setCurrencyCount("+" + currencyCustomerLogDO.getCurrencyCount().toString());
                    currencyCustomerLogDTO.setSign(CurrencyCustomerLogEnumConstants.ENUM_SIGN_INCREASE);
                } else {
                    logger.error("CurrencyCustomerLogServiceImpl.details()-----通过payId获取积分信息参数，对应的payid为：" + payId);
                    Map<String, String> condMap = payInfoFeign.getOrderInfoCounterNameCurrencyType(payId);
                    if(condMap==null || condMap.size()<1){
                        return new ArrayList<>();
                    }else{
                        logger.error("CurrencyCustomerLogServiceImpl.details()-----通过payId获取积分信息参数：" + condMap.toString());
                        currencyCustomerLogDTO.setStoreName(condMap.get("storeName"));
                        currencyCustomerLogDTO.setOrderId(condMap.get("orderId"));
                        String currencyType = condMap.get("currencyType");
                        /*判断是否是退款业务*/
                        if(currencyCustomerLogDO.getCurrencySource().equalsIgnoreCase(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_CURRENCYREFOUNDRETURNCURRENCY)){
                            /*积分退款增加积分*/
                            currencyCustomerLogDTO.setActivityType(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_CURRENCYREFOUNDRETURN);
                            currencyCustomerLogDTO.setCurrencyCount("+" + currencyCustomerLogDO.getCurrencyCount().toString());
                            currencyCustomerLogDTO.setSign(CurrencyCustomerLogEnumConstants.ENUM_SIGN_INCREASE);
                        }else if(currencyCustomerLogDO.getCurrencySource().equalsIgnoreCase(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_CASHREFOUNDREDUCECURRENCY)){
                            /*现金退款扣除减积分*/
                            currencyCustomerLogDTO.setActivityType(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_CASHREFOUND);
                            currencyCustomerLogDTO.setCurrencyCount("-" + currencyCustomerLogDO.getCurrencyCount().toString());
                            currencyCustomerLogDTO.setSign(CurrencyCustomerLogEnumConstants.ENUM_SIGN_REDUCE);
                        }else{
                            /*其他类型*/
                            /*现金支付送积分*/
                            if (currencyCustomerLogDO.getCurrencyType().equals("01")) {
                                currencyCustomerLogDTO.setActivityType(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_CASHFOLDBACKCURRENCY);
                                currencyCustomerLogDTO.setCurrencyCount("+" + currencyCustomerLogDO.getCurrencyCount().toString());
                                currencyCustomerLogDTO.setSign(CurrencyCustomerLogEnumConstants.ENUM_SIGN_INCREASE);
                            } else{
                                /*积分支付扣除积分*/
                                if (currencyType.equals("1")) {
                                    currencyCustomerLogDTO.setActivityType(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_CURRENCYPAY);
                                    currencyCustomerLogDTO.setCurrencyCount("-" + currencyCustomerLogDO.getCurrencyCount().toString());
                                    currencyCustomerLogDTO.setSign(CurrencyCustomerLogEnumConstants.ENUM_SIGN_REDUCE);
                                } else if (currencyType.equals("0")) {
                                    /*积分代扣扣除商品积分*/
                                    currencyCustomerLogDTO.setActivityType(CurrencyCustomerLogEnumConstants.ENUM_CURRENCYTYPE_CURRENCYREPURCHASE);
                                    currencyCustomerLogDTO.setCurrencyCount("-" + currencyCustomerLogDO.getCurrencyCount().toString());
                                    currencyCustomerLogDTO.setSign(CurrencyCustomerLogEnumConstants.ENUM_SIGN_REDUCE);
                                }

                            }
                        }

                    }
                }
                currencyCustomerLogList.add(currencyCustomerLogDTO);
            }
            logger.error("CurrencyCustomerLogServiceImpl.details()-----通过payId获取积分信息参数currencyCustomerLogList：" + currencyCustomerLogList.toString());
            return currencyCustomerLogList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int detailsCount(String customerId) {

        return currencyCustomerLogDao.detailsCount(customerId);
    }

}
